package com.banshou.installs.ui.activity;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Dialog;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.banshou.installs.R;
import com.banshou.installs.constant.BaseApplication;
import com.banshou.installs.constant.Config;
import com.banshou.installs.model.dao.PrograssEvent;
import com.banshou.installs.model.dao.ResponseUpdate;
import com.banshou.installs.model.net.Observer;
import com.banshou.installs.model.net.ResponseInfo;
import com.banshou.installs.model.net.RetrofitManager;
import com.banshou.installs.model.net.RxUtil;
import com.banshou.installs.ui.fragment.HomeFragment;
import com.banshou.installs.ui.fragment.SelfFragment;
import com.banshou.installs.utils.RxBus;
import com.banshou.installs.utils.ToastUtil;
import com.banshou.installs.utils.UpdateUtil;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends BaseActivity {
    @BindView(R.id.back)
    RelativeLayout back;
    @BindView(R.id.pager_title)
    TextView pagerTitle;
    @BindView(R.id.right_title)
    TextView rightTitle;
    @BindView(R.id.main_fl)
    FrameLayout mainFl;
    @BindView(R.id.main_ll)
    LinearLayout mainLl;
    @BindView(R.id.main_rlhoome)
    RelativeLayout mainRlhoome;
    @BindView(R.id.main_rlself)
    RelativeLayout mainRlself;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.tv2)
    TextView tv2;
    private int position;
    private static boolean isExit = false;
    private Fragment mCurentFragment;
    private Class[] fragmentClazz = new Class[]{
            HomeFragment.class, SelfFragment.class};

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        ActivityUtils.finishAllActivitiesExceptNewest();
        back.setVisibility(View.GONE);
        changeFragment(0);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @SuppressLint("HandlerLeak")
    private static Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出屏宝宝",
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 3000);
        } else {
            ActivityUtils.finishAllActivities();
        }
    }


    @OnClick({R.id.main_rlhoome, R.id.main_rlself})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.main_rlhoome:
                if (position == 0) {
                    return;
                }
                position = 0;
                view1.setBackgroundColor(getResources().getColor(R.color.color_login_button));
                view2.setBackgroundColor(getResources().getColor(R.color.color_mainline));
                tv1.setEnabled(false);
                tv2.setEnabled(true);
                changeFragment(position);
                break;
            case R.id.main_rlself:
                if (position == 1) {
                    return;
                }
                position = 1;
                view2.setBackgroundColor(getResources().getColor(R.color.color_login_button));
                view1.setBackgroundColor(getResources().getColor(R.color.color_mainline));
                tv1.setEnabled(true);
                tv2.setEnabled(false);
                changeFragment(position);
                break;
        }
    }

    /**
     * 底部导航 - 更改fragment
     */
    private void changeFragment(int index) {
        if(index == 0){
            pagerTitle.setText(R.string.main_left);
        }else{
            pagerTitle.setText(R.string.main_right);
        }
        String tag = fragmentClazz[index].getName();
        try {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            if (mCurentFragment != null) {
                ft.hide(mCurentFragment);
            }
            Fragment fragment = fm.findFragmentByTag(tag);
            if (fragment == null) {
                fragment = (Fragment) fragmentClazz[index].newInstance();
                ft.add(R.id.main_fl, fragment, tag);
            } else {
                ft.show(fragment);
            }
            mCurentFragment = fragment;
            ft.commit();
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("error",e.toString());
        }
    }

}
